/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO_BUS;

import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author MinhNhat
 */
public class Database_Server {

    /**
     * @param args the command line arguments
     */
    public static Vector Tables;
    public static Document doc;

    public static void main(String[] args) {
    //public Database_Server() {
        try {
            // TODO code application logic here
            // String temp = "CREATE TABLE Persons ( P_Id int Primary Key, LastName varchar(255), FirstName varchar(255),Address varchar(255), City varchar(255))";
            // String temp = "INSERT INTO Persons VALUES (3,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')";
            //String temp="DELETE FROM Persons WHERE LastName=";//'Nilsen' AND FirstName='Johan'";        
            String temp = "UPDATE HocSinh SET Maso=2";
           // String temp = "SELECT * FROM Persons Where P_Id > 2";
            // String temp = "CREATE TABLE Jobs (Id int, Name String )";
            //String temp = "INSERT INTO Jobs VALUES (1,'Rac')";
            // String temp = "INSERT abc";
           // CryptoFile.Decrypt("30532228", temp);
            File file = new File("Database.xml");
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            doc = docBuilder.parse(file);
            Tables = LoadDatabase(doc);
            
            MySentence Sentence = GetSentenceFromString(temp);
            // Sentence.Print();

            ((SelectSentence)Sentence).Excute(doc, Tables, new Vector());


            /*
             * Transformer transformer =
             * TransformerFactory.newInstance().newTransformer();
             * transformer.setOutputProperty(OutputKeys.INDENT, "yes");
             *
             * StreamResult result = new StreamResult(new StringWriter());
             * DOMSource source = new DOMSource(doc);
             * transformer.transform(source, result);
             *
             * String xmlOutput = result.getWriter().toString();
             * System.out.println(xmlOutput);
             */
            TransformerFactory factory = TransformerFactory.newInstance();

            Transformer transformer = factory.newTransformer();
            Result result = new StreamResult(new File("Database.xml"));

            Source source = new DOMSource(doc);

            transformer.transform(source, result);

        } catch (TransformerException ex) {
            Logger.getLogger(Database_Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Database_Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Database_Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Database_Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static MySentence GetSentenceFromString(String str) {
        MySentence sentence = null;

        if (SelectSentence.isSelectSentence(str)) {
            sentence = new SelectSentence(str);
            return sentence;
        }

        if (DeleteSentence.isDeleteSentence(str)) {
            sentence = new DeleteSentence(str);
            return sentence;
        }

        if (UpdateSentence.isUpdateSentence(str)) {
            sentence = new UpdateSentence(str);
            return sentence;
        }

        if (InsertSentence.isInsertSentence(str)) {
            sentence = new InsertSentence(str);
            return sentence;
        }

        if (CreateSentence.isCreateSentence(str)) {
            sentence = new CreateSentence(str);
            return sentence;
        }

        return sentence;
    }

    public static Vector LoadDatabase(Document doc) {
        Vector tables = new Vector();
        NodeList list = doc.getElementsByTagName("Table");
        for (int i = 0; i < list.getLength(); i++) {
            Element element = (Element) list.item(i);
            MyTable table = new MyTable(element);
            tables.add(table);
        }
        return tables;
    }
}
